package com.cashmate.struts;

import com.cashmate.hibernate.dao.DocumentDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.dao.RequestDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.hibernate.pojo.Request;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

@Result(location = "${loc}", name = "success", type = "redirect")
public class Request_SaveRequest extends ActionSupport {
    private static Logger logger = Logger.getLogger(Request_SaveRequest.class);
    private LoanverificationDAO loanverDAO = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private RequestDAO requestDAO = (RequestDAO) ServletActionContext.getServletContext().getAttribute("requestDAO");
    private DocumentDAO documentDAO = (DocumentDAO) ServletActionContext.getServletContext().getAttribute("documentDAO");
    private String selected;
    private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    private String loc;

    @Action(value = "/saverequest")
    @Override
    public String execute() throws Exception {
        String date = formatter.format(new Date());
        loc = "/loanrequest.jsp?cat=branchcash";
        try {
            List<Loanverification> loanvers = new ArrayList<Loanverification>();
            String[] sel = selected.split(",");
            Integer[] sel_int = new Integer[sel.length];
            int count = 0;
            for (String s : sel) {
                sel_int[count] = Integer.parseInt(s);
                count++;
            }
            
            DetachedCriteria criteria = DetachedCriteria.forClass(Loanverification.class);
            criteria.add(Restrictions.in("loanverificationid", sel_int));
            int loanSize = loanverDAO.countByCriteria(criteria);
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);
            loanvers = loanverDAO.findByCriteria(criteria, 0, loanSize);
            
            Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");
            
            List<Request> reqs = new ArrayList<Request>();
            DetachedCriteria criteria_request = DetachedCriteria.forClass(Request.class);
            criteria_request.add(Restrictions.eq("requestdate", date));
            int reqSize = requestDAO.countByCriteria(criteria_request);
            criteria_request.setProjection(null);
            criteria_request.setResultTransformer(Criteria.ROOT_ENTITY);
            reqs = requestDAO.findByCriteria(criteria_request, 0, reqSize);
            
            Request req = new Request();
            if(reqs.isEmpty()){                
                req.setStatus("Verified");
                req.setRequestdate(formatter.format(new Date()));
                req.setBranches(b);
                req.setEmployees((Employees) ServletActionContext.getRequest().getSession().getAttribute("employee"));
                requestDAO.insert(req);

                Documents doc = new Documents();
                doc.setDocumenttype("Loan Request");
                doc.setDocumentdate(formatter.format(new Date()));
                doc.setDocumentno(b.getBranchcode() + req.getRequestid());
                doc.setIsactive(true);
                documentDAO.insert(doc);

                req.setDocuments(doc);
                requestDAO.update(req);
            }
            
            else{
                for(Request r : reqs){
                    req = r;
                }
            }

            for (Loanverification l : loanvers) {
                l.setStatus("Verified");
                l.setRequest(req);
                loanverDAO.update(l);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return SUCCESS;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public String getLoc() {
        return loc;
    }
}
